#if defined _hltv_included
#endinput
#endif
#define _hltv_included


stock hltvrcon[12] 	//contains the callenge number
stock hltv_data_received
stock hltv_data[256]
stock s_handle
stock s_error
stock recvattempts
stock recvattempts2
stock g_hltvpw[40]
stock g_hltvcmd[60]
//stock hltv_connect(hltvip[],hltvport,hltvpw,hltvcmd)



public  hltv_cmd(hltvip[],hltvport,hltvpw[],hltvcmd[])
{ 
	hltv_data_received=0
	format(hltv_data,charsmax(hltv_data),"")
	if (s_handle)	// if socket is already open, break
		socket_close(s_handle)
	
	s_handle = socket_open(hltvip,hltvport,SOCKET_UDP,s_error)	//open socket
	
	if (s_error) {
		hltv_data_received=-1
		s_handle = 0
		return PLUGIN_HANDLED
	}
	format(g_hltvpw,charsmax(g_hltvpw),"%s",hltvpw)
	format(g_hltvcmd,charsmax(g_hltvcmd),"%s",hltvcmd)
	set_task(0.1,"send_challenge_hltv")
	return PLUGIN_CONTINUE
}
public  send_challenge_hltv(id) {
	new packetstr[32]
	formatex (packetstr,31,"%c%c%c%cchallenge rcon^n",-1,-1,-1,-1)//send string to get challenge number for hltv
	socket_send2(s_handle, packetstr, 31)
	set_task(0.1, "get_rconquery")
}
public get_rconquery() {
	if (!s_handle) { //sock open?
		hltv_data_received=-1
		return PLUGIN_CONTINUE
	}
	
	if (!socket_change(s_handle, 1))
	{
		recvattempts += 1
		if (recvattempts > 5) 
		{ 
			hltv_data_received=-1
			abort_query()
			recvattempts = 0
			return PLUGIN_HANDLED
		} 
		else 
		{
			set_task(0.2, "get_rconquery")
			return PLUGIN_CONTINUE
		}
	}
	recvattempts = 0
	
	new packet[32]
	socket_recv(s_handle, packet, 31) //get response data from sock
	if (!equal(packet, {-1,-1,-1,-1,'c'}, 5)) { // hltv is sending challengenr? if not error
		hltv_data_received=-1
		abort_query()
		return PLUGIN_HANDLED
	}
	
	new i=0
	new offset = 19 //string offset
	while(47 < packet[i+offset] < 58) { //get only and all numbers from response
		copy(hltvrcon[i],1,packet[i+offset]) //build challenge number
		i++
	}
	i=0
	set_task(0.1,"send_command")
	
	return PLUGIN_CONTINUE
}
public  send_command() {
	new send[256]
	setc(send, 255, 0x00)
	setc(send, 4, 0xff)
		
		
	formatex(send[4], 255, "rcon %s %s %s ^n", hltvrcon,g_hltvpw,g_hltvcmd)
	socket_send2(s_handle, send, 255)
	set_task(0.1,"read_hltv_data")
	//abort_query() //close socket
	
	return PLUGIN_CONTINUE
}
public  read_hltv_data()
{
	
	if (!s_handle) { //sock open?
		return PLUGIN_CONTINUE
	}

	if (!socket_change(s_handle, 1))
	{
		recvattempts += 1
		if (recvattempts > 50) 
		{ 
			hltv_data_received=-1
			abort_query()
			recvattempts = 0
			return PLUGIN_HANDLED
		} 
		else 
		{
			set_task(0.1, "read_hltv_data")
			return PLUGIN_CONTINUE
		}
	}
	recvattempts = 0

	socket_recv(s_handle, hltv_data,charsmax(hltv_data)) //get response data from sock
	copy(hltv_data,charsmax(hltv_data), hltv_data[5])

	hltv_data_received=1
	abort_query()
	
	return PLUGIN_CONTINUE
}

public  abort_query() {
	socket_close(s_handle)
	s_handle=0
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1049\\ f0\\ fs16 \n\\ par }
*/
